clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output


// open visit dataset
forval yr = 2005(1)2009 {
	disp "year: `yr'"
	tempfile tmpdata
	shell nice gunzip Raw/members/member_month_`yr'_clean.dta -c > `tmpdata'
	append using `tmpdata', keep(member_key yearmo coverage_medical primary_ins payercode std_product_type insurance_type tier sex age member_zip)
}

// Keep only medical coverage
keep if coverage_medical==1

gen male = (sex==1)

// Merge on payer info
merge m:1 payercode using build/payers_clean.dta
drop if _merge==2
drop _merge

// Covert payercode to numeric
egen payer_id = group(payercode)
drop payercode

sort member_key yearmo coverage_medical primary_ins
duplicates drop member_key yearmo coverage_medical primary_ins, force

// Merge on member zip code characteristics
rename member_zip zip
merge m:1 zip using zip_info/Zip_chars.dta
drop if _merge==2
drop _merge zip_name zcta_lat zcta_long
rename zip member_zip
rename in_nh member_in_nh

keep if member_in_nh==1


gen year = floor(yearmo/100)
gen month = yearmo - year*100
gen ym = ym(year,month)
format ym %tm
drop year month
compress

drop if ym>ym(2009,3)

gen total_members=1


// Make dummy vars
gen payer_name_abbr = payernm_short
label values payer_name_abbr payernm_short
replace payer_name_abbr = 7 if inlist(payernm_short,1,8) // Relabel Aetna and UnitedHealth as other
replace payer_name_abbr = 3 if payernm_short==4 // Relabel Connect General as Cigna


xi i.std_product_type i.payer_name_abbr i.rural_class, noomit
compress

// Collapse
collapse (sum) total_members _Istd_pro* _Ipayer_nam* age male zip_pct_hs zip_pct_ba zip_median_income zip_mean_income _Irural* , by(ym)

gen post = ym>=ym(2007,3)
label var post "$\text{Post}\textsubscript{t}$"


// Examine Product Type
preserve
*drop if ym>=ym(2008,3)
drop if ym<=ym(2005,3)
*drop _Irural_cla_1 _Irural_cla_2 _Ipayer_nam_2

local cnts = "age male zip_pct_hs zip_pct_ba zip_median_income" 

// ppo
reg _Istd_produ_1 post ym `cnts', vce(cl post)
summ _Istd_produ_1 if e(sample), meanonly
estadd scalar ymean = r(mean), replace
eststo a1

// pos
reg _Istd_produ_2 post ym  `cnts', vce(cl post)
summ _Istd_produ_2 if e(sample), meanonly
estadd scalar ymean = r(mean), replace
eststo a2

// hmo
reg _Istd_produ_3 post ym  `cnts', vce(cl post)
summ _Istd_produ_3 if e(sample), meanonly
estadd scalar ymean = r(mean), replace
eststo a3

// epo
reg _Istd_produ_8 post ym  `cnts', vce(cl post)
summ _Istd_produ_8 if e(sample), meanonly
estadd scalar ymean = r(mean), replace
eststo a4

// Indemnity
reg _Istd_produ_11 post ym  `cnts', vce(cl post)
summ _Istd_produ_11 if e(sample), meanonly
estadd scalar ymean = r(mean), replace
eststo a5
restore

// Output
esttab a1 a2 a3 a4 a5 using $output/insurer_event_study_regs_a.tex, frag cells(b(fmt(%8.0fc) star) se(fmt(%8.0fc) par)) ///
	keep(post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\)  ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a1 a2 a3 a4 a5 using $output/insurer_event_study_regs_b.tex, frag cells(b(fmt(3) star)) ///
	drop(*)  stats(ymean r2 N, fmt(%12.0fc %6.3fc %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 




	
// Examine Firm
preserve
drop if ym<=ym(2005,3)

// Anthem
local cnts = "age male zip_pct_hs zip_pct_ba zip_median_income" 
reg _Ipayer_nam_2 ym post `cnts', vce(cl post)
summ _Ipayer_nam_2 if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo b1

// Cigna
reg _Ipayer_nam_3 ym post `cnts', vce(cl post)
summ _Ipayer_nam_3 if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo b2

// Harvard Pilgrim
reg _Ipayer_nam_5 ym post `cnts', vce(cl post)
summ _Ipayer_nam_5 if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo b3

// Matthew Thornton
reg _Ipayer_nam_6 ym post `cnts', vce(cl post)
summ _Ipayer_nam_6 if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo b4

// Other
reg _Ipayer_nam_7 ym post `cnts', vce(cl post)
summ _Ipayer_nam_7 if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo b5
restore


// Output
esttab b1 b2 b3 b4 b5 using $output/insurer_event_study_regs_c.tex, frag cells(b(fmt(%8.0fc) star) se(fmt(%8.0fc) par)) ///
	keep(post)  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\)  ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
	
esttab b1 b2 b3 b4 b5 using $output/insurer_event_study_regs_d.tex, frag cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(%12.0fc %6.3fc %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 
